<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>斐波拉契数列</title>
</head>

<body>

</body>
<script>
    /*     let Fib = (number) => {
            if (number <= 1) {
                return 1
            }
            return Fib(number - 1) + Fib(number - 2)
        }
        console.log(Fib(5)); */

   /*  let Fib = (number, a1 = 1, a2 = 1) => {
        if (number <= 1) {
            return a2
        }
        return Fib(number - 1, a2, a1 + a2)
    }
    console.log(Fib(5)); */

    // es6 实现斐波拉契数列

    class Fib {
        constructor(num) {
            this.num = num
        }
        *[Symbol.iterator]() {
            let [a, b] = [0, 1]
            while (true) {
                [a, b] = [b, a + b]
                if (this.num < a) {
                    return
                }
                yield a
            }
        }
    }
    let fib = new Fib(5)
    for (let i of fib) {
        console.log(i);
    }
</script>

</html>